﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;

namespace iPdf
{
    public class FileFormat
    {
        /// <summary>
        /// some predefined formats
        /// </summary>
        public static readonly FileFormat WordFormat = new
            FileFormat("Word Documents", "*.doc;*.docx");
        public static readonly FileFormat ExcelFormat = new
            FileFormat("Excel Documents", "*.xls;*.xlsx");
        public static readonly FileFormat PowerPointFormat = new
            FileFormat("Powerpoint Documents", "*.ppt;*.pptx");
        public static readonly FileFormat OfficeFormats = new
            FileFormat("Office Documents", "*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx");
        public static readonly FileFormat XmlFormat = new FileFormat("Xml Documents", "*.xml");
        public static readonly FileFormat PdfFormat = new FileFormat("PDF Documents", "*.pdf");
        public static readonly FileFormat PfxFormat = new FileFormat("PFX Files", "*.pfx;*.p12");
        public static readonly FileFormat Images = new FileFormat("Image Files", "*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.ico;*.icon;*.wmf;*.emf;*.exif;*.tif;*.tiff");
        public static readonly FileFormat TiffImage = new FileFormat("Tiff Images", "*.tif;*.tiff");

        public string Description;
        public string Filter;

        public string FilterString
        {
            get
            {
                char separator = '|';
                return Description + separator + Filter;
            }
        }

        public string[] Extensions
        {
            get
            {
                string[] extensions = Filter.Split(Helper.SemicolonChar);
                for (int i = 0; i < extensions.Length; ++i)
                {
                    string ext = extensions[i].Trim();
                    if (!string.IsNullOrEmpty(ext))
                    {
                        extensions[i] = ext.Substring(1).ToLower();
                    }
                }

                return extensions;
            }
        }

        public string Extension
        {
            get
            {
                string[] extensions = Extensions;
                return extensions.Length > 0 ? extensions[0] : string.Empty;
            }
        }

        public Collection<FileFormat> SubFormats
        {
            get
            {
                Collection<FileFormat> subFormats = new Collection<FileFormat>();

                string[] subFilters = Filter.Split(Helper.SemicolonChar);

                foreach (string subFilter in subFilters)
                {
                    string fileSuffix = subFilter.Substring(2);
                    string description = fileSuffix.ToUpper() + " Files";
                    subFormats.Add(new FileFormat(description, subFilter));
                }

                return subFormats;
            }
        }

        public bool IsOneOf(string fileName)
        {
            string ext = Path.GetExtension(fileName).ToLower();
            return Array.IndexOf(Extensions, ext) != -1;
        }

        public Collection<string> Wildcards
        {
            get
            {
                Collection<string> wildcards = new Collection<string>();
                string[] splitResults = Filter.Split(Helper.SemicolonChar);
                foreach(string str in splitResults)
                {
                    wildcards.Add(str);
                }

                int removedCnt = 0;
                int count = wildcards.Count;
                for (int i = 0; i < count; ++i)
                {
                    int adjustedIndex = i - removedCnt;
                    for (int j = 0; j < adjustedIndex; ++j)
                    {
                        if (wildcards[adjustedIndex].StartsWith(wildcards[j]))
                        {
                            wildcards.RemoveAt(adjustedIndex);
                            ++removedCnt;
                            break;
                        }
                    }
                }

                return wildcards;
            }
        }

        public FileFormat()
        {
            Description = string.Empty;
            Filter = string.Empty;
        }

        public FileFormat(string description, string filter)
        {
            Description = description;
            Filter = filter;
        }

        public FileFormat(string fileName)
        {
            string ext = Path.GetExtension(fileName).ToLower();
            if(string.IsNullOrEmpty( ext ) )
            {
                Description = string.Empty;
                Filter = string.Empty;
                return;
            }

            string suffix = ext.Substring(1).ToUpper();
            Description = suffix + " Files";
            Filter = Helper.Asterisk + ext;
        }

        public override bool Equals(object obj)
        {
            if (obj is FileFormat)
            {
                FileFormat rhs = (FileFormat)obj;
                return Filter == rhs.Filter;
            }

            return false;
        }

        public override int GetHashCode()
        {
            return Filter.GetHashCode();
        }

        public void AddExtension(string extension)
        {
            extension.ToLower();
            if(!extension.StartsWith(Helper.Dot))
            {
                extension = Helper.Dot + extension;
            }

            extension =  Helper.Asterisk + extension;
            if (Filter.Length > 0)
            {
                extension = Helper.Semicolon + extension;
            }

            Filter +=  extension;
        }

    }
}
